package cn.lix.sc.rpc.pdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.*;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Locale;


public class PDFHelper {

    public static final String ENCODING = "utf-8";


    /**
     * 获取模板内容
     * @param templateName      模板文件名
     * @param param          模板参数
     * @return
     * @throws Exception
     */
    public static String getTemplateContent(String templateName, Object param) {
        Configuration configuration = new Configuration();
        Writer out = null;
        try {
            configuration.setClassForTemplateLoading(PDFHelper.class, "/templates");
            // 设置编码格式
            configuration.setEncoding(Locale.CHINA, ENCODING);
            out = new StringWriter();
            Template template = configuration.getTemplate(templateName);
            template.process(param, out);
            out.flush();
            return out.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
    }

    /**
     * HTML 转 PDF
     * @param content html内容
     * @param outPath 输出pdf路径
     * @return 是否创建成功
     */
    public static boolean html2Pdf(String content, String outPath) {
        try {
            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outPath));
            document.open();
            XMLWorkerHelper.getInstance().parseXHtml(writer,
                    document,
                    new ByteArrayInputStream(content.getBytes()),
                    XMLWorkerHelper.class.getResourceAsStream("/default.css"),
                    Charset.forName(ENCODING),
                    new CustomXMLWorkerFontProvider());
            document.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return true;
    }
    /**
     * HTML 转 PDF
     * @param content html内容
     * @return PDF字节数组
     */
    public static byte[] html2Pdf(String content, Image... imageList) {
        ByteArrayOutputStream outputStream = null;
        try {
            Document document = new Document();
            outputStream = new ByteArrayOutputStream();
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
            document.open();
            XMLWorkerHelper.getInstance().parseXHtml(writer,
                    document,
                    new ByteArrayInputStream(content.getBytes()),
                    XMLWorkerHelper.class.getResourceAsStream("/default.css"),
                    Charset.forName(ENCODING),
                    new CustomXMLWorkerFontProvider());
            if (imageList != null && imageList.length > 0) {
                for (Image image : imageList) {
                    document.add(image);
                }
            }
            document.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return outputStream.toByteArray();
    }

}
